#################
# Appendix: UK  #    
# Figure 4 & 8  #
#################

###load library
library(openxlsx)
library(psych)
library(ggplot2)
library(gridExtra)
library(dtw)
library(tseries)
library(TSclust)
library(tidyverse)
library(zoo)
source("extract.R")



#######----------Figure 4: All patterns of indices (the UK)--------##########


#######################Laakso Taagepera ENPP###########################

df_mp_uk<-read.csv("UK_FINAL.csv")
attach(df_mp_uk)
summary(df_mp_uk)
df_mp_uk$percon<-df_mp_uk$dfmean_con/df_mp_uk$allp_uk


df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(effpar_ele/2)*((allp_uk/perindependent)*(1/effpar_ele^2))
describe(df_mp_uk$macropartisanship1)

#weighting process
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

df_mp_uk$macropartisanship_UK_LT<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)

###-------without nested s2


df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(effpar_ele/2)*(allp_uk/perindependent)
describe(df_mp_uk$macropartisanship1)

#weighting process
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

df_mp_uk$macropartisanship_UK_LT_withoutnested<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)

###-----simply product the proportion of independent 

df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(effpar_ele/2)*perindependent
describe(df_mp_uk$macropartisanship1)

#weighting process
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

df_mp_uk$macropartisanship_UK_LT_prindependent<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)





###########################Molinar############################


df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(enpp_np/2)*((allp_uk/perindependent)*(1/enpp_np^2))
describe(df_mp_uk$macropartisanship1)

#weighting process
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

df_mp_uk$macropartisanship_UK_NP<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)

###-------without nested s2

df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(enpp_np/2)*(allp_uk/perindependent)
describe(df_mp_uk$macropartisanship1)

#weighting process
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

df_mp_uk$macropartisanship_UK_NP_withoutnested<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)


###-----simply product the proportion of independent 

df_mp_uk$macropartisanship1<-(dfmean_con.mean.Con/df_mp_uk$allp_uk)*(enpp_np/2)*perindependent
describe(df_mp_uk$macropartisanship1)

#weighting process
df_mp_uk$macropartisanship1<-ifelse((is.infinite(df_mp_uk$macropartisanship1) & (df_mp_uk$macropartisanship1> 0)) , 0.01, df_mp_uk$macropartisanship1)
macrop_var<-var(df_mp_uk$macropartisanship1)
macroper_var<-var(df_mp_uk$percon)

df_mp_uk$macropartisanship_UK_NP_prindependent<-(df_mp_uk$macropartisanship1/macrop_var+df_mp_uk$percon/macroper_var)/
  (1/macrop_var+1/macroper_var)



###constituting data frame for comparison

cons<-df_mp_uk$dfmean_con.mean.Con/100

df_mp_enpp<-data.frame(cons,df_mp_uk$yearMon,
                       df_mp_uk$macropartisanship_UK_LT,
                       df_mp_uk$macropartisanship_UK_LT_withoutnested,
                       df_mp_uk$macropartisanship_UK_LT_prindependent,
                       df_mp_uk$macropartisanship_UK_NP,
                       df_mp_uk$macropartisanship_UK_NP_withoutnested,
                       df_mp_uk$macropartisanship_UK_NP_prindependent)

##################mp with DRA##############################


#load data
uk<-read.csv("uk_extract.csv")
attach(uk)
describe(uk)
uk<-na.omit(uk)

#make tidy data by tidyverse
df_uk_tidy <- uk %>%
  gather(key = VARNAME, value = value, -yearMon)

#setting time-series information
varname<-df_uk_tidy$VARNAME
date<-as.Date(df_uk_tidy$yearMon)
index<-(df_uk_tidy$value)/100
ncases<-NULL

#using "extract" to compute macropartisanship
output_uk<-extract(varname,date=date,index,ncases,begindt=ISOdate(1959,10,1),
                   enddt = ISOdate(2016,12,1), unit="M",npass=2)
display(output_uk)
summary(output_uk)

df_ld_uk<-data.frame(output_uk$varname, output_uk$loadings1,output_uk$loadings2)
colnames(df_ld_uk)<-c("Party names","Loadings1","Loadings2")
print(xtable(df_ld_uk), include.rownames=FALSE)

#factor scores of 2 dimensions
mpartisan1_uk_DRA<-output_uk$latent1
mpartisan2_uk<-output_uk$latent2

yearMon<-as.Date(sprintf("%.2f.01", output_uk$period), format = "%Y.%m.%d")

###constituting data frame for comparison
df_dra_uk<-data.frame(as.factor(yearMon),mpartisan1_uk_DRA)
colnames(df_dra_uk)<-c("df_mp_uk.yearMon","mpartisan1_uk_DRA")

#join two data.frame
tb_uk_mpcomparison<-inner_join(df_dra_uk,df_mp_enpp,by=c("df_mp_uk.yearMon"))
write.csv(tb_uk_mpcomparison, "tb_uk_mpcomparison.csv")


##################plot all indices#####################
#plot the simple macropartisanship and the modified
tidy_all_indices<-tb_uk_mpcomparison %>% 
  gather(key="variable", value="value", -df_mp_uk.yearMon)

ggplot(tidy_all_indices, aes(as.yearmon(x = df_mp_uk.yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  theme_minimal()



#######---------Figure 8: Dynamic time warping distance for the UK case----------########

##################DTW############################

windows(30,25)
pdf("fig_dtw_uk.pdf")
split.screen(c(3,3))

###----displaying dynamic time warping:

###LT indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_uk_mpcomparison$cons ,tb_uk_mpcomparison$df_mp_uk.macropartisanship_UK_LT_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_uk_mpcomparison.cons
ts_b<-df_dtw$tb_uk_mpcomparison.df_mp_uk.macropartisanship_UK_LT_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(1)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-indep", cex.main = 0.6)


###NP indep
#simply calulating dtw distance
df_dtw<-data.frame(tb_uk_mpcomparison$cons ,tb_uk_mpcomparison$df_mp_uk.macropartisanship_UK_NP_prindependent)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_uk_mpcomparison.cons
ts_b<-df_dtw$tb_uk_mpcomparison.df_mp_uk.macropartisanship_UK_NP_prindependent

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(2)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-indep", cex.main = 0.6)


###LT simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_uk_mpcomparison$cons ,tb_uk_mpcomparison$df_mp_uk.macropartisanship_UK_LT_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_uk_mpcomparison.cons
ts_b<-df_dtw$tb_uk_mpcomparison.df_mp_uk.macropartisanship_UK_LT_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance



screen(3)
dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-simple", cex.main = 0.6)


###NP simple
#simply calulating dtw distance
df_dtw<-data.frame(tb_uk_mpcomparison$cons ,tb_uk_mpcomparison$df_mp_uk.macropartisanship_UK_NP_withoutnested)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_uk_mpcomparison.cons
ts_b<-df_dtw$tb_uk_mpcomparison.df_mp_uk.macropartisanship_UK_NP_withoutnested

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(4)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-simple", cex.main = 0.6)


###LT nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_uk_mpcomparison$cons ,tb_uk_mpcomparison$df_mp_uk.macropartisanship_UK_LT)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_uk_mpcomparison.cons
ts_b<-df_dtw$tb_uk_mpcomparison.df_mp_uk.macropartisanship_UK_LT

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(5)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs LT-nested", cex.main = 0.6)


###NP nested
#simply calulating dtw distance
df_dtw<-data.frame(tb_uk_mpcomparison$cons ,tb_uk_mpcomparison$df_mp_uk.macropartisanship_UK_NP)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_uk_mpcomparison.cons
ts_b<-df_dtw$tb_uk_mpcomparison.df_mp_uk.macropartisanship_UK_NP

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance


screen(6)

dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs NP-nested", cex.main = 0.6)




####DRA
#simply calulating dtw distance
df_dtw<-data.frame(tb_uk_mpcomparison$cons ,tb_uk_mpcomparison$mpartisan1_uk_DRA)
df_dtw<-na.omit(df_dtw)

ts_a<-df_dtw$tb_uk_mpcomparison.cons
ts_b<-df_dtw$tb_uk_mpcomparison.mpartisan1_uk_DRA

d2 <- dtw::dtw(ts_a, ts_b,step.pattern = symmetric1)
d2$distance

screen(7)


dtwPlotTwoWay(d2, ts_a, ts_b,main="CONVENTIONAL vs DRA", cex.main = 0.6)


dev.off()